---
output: pdf_document
title: "Appendix E: Belt and Road and UNGA Voting - Robustness"
geometry: margin=1in
mainfont: cochineal
fontsize: 11pt
linestretch: 1.15
endnote: no
sansitup: no
graphics: yes
toc: yes

header-includes:
- \usepackage{float} #use the 'float' package
- \floatplacement{figure}{H} #make every figure with caption = h
- \usepackage{graphicx}
- \usepackage{longtable}
- \LTcapwidth=.95\textwidth
- \linespread{1.05}
- \usepackage{hyperref}
- \usepackage{booktabs}
- \usepackage{subfig}
- \renewcommand{\figurename}{Figure B.}
- \makeatletter
- \def\fnum@figure{\figurename\thefigure}
- \makeatother

subtitle: 'Cluster analysis'

---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F, warning = F, comment = F, message = F, fig.show = 'hide', fig.ncol = 1, fig.align = "center")

library(tidyverse)
library(giscoR)
library(zoo)
library(gsynth)

```


# Analysis of China - Varying the cut-off - level 0.025


```{r}
data_china <- read_csv2("./replication_files/data/data/final_data/china_data_with_cluster_co_0025.csv") %>% filter(year > 2003)

data_china %>% distinct(ctry1, cluster) %>% arrange(cluster)


```

```{r}
df <- data_china %>% drop_na(IdealPointDistance)

df <- df %>% drop_na(polity2_dist, gdp_cap_dist)

df <- df %>% group_by(ctry1) %>% mutate(n = n()) 


df_sub <- df %>% filter(cluster == 3 & ctry1 != 'Canada')
table <- df_sub %>% select(ctry1) %>% distinct()
```


```{r}
knitr::kable(table)
```

```{r}
# Pre-treatment trends
ggplot(df_sub %>% filter(year %in% c(2004:2013)), aes(year, IdealPointDistance, color = ctry1)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = 'none') +
  
  labs(x = 'Year')

#ggsave('./graphs/china/pre_treatment_co_0025.pdf', width = 7, height = 3)


```


```{r, }
#### Reproduces Figure A.11 ####
m1 <- gsynth(IdealPointDistance ~ treated + ctry1_fariss + log(ctry1_gdp_cap) + ctry1_polity2, 
             data = df_sub, 
             index = c("ctry1", "year"),
             se = T,
             force = 'time',
            # CV = TRUE,
             r = c(0, 5),
             inference = "parametric", nboots = 1000,
             parallel = FALSE)


results <- as.tibble(m1$est.att) %>%
  mutate(n = row_number()) %>%
  mutate(ci_90_low = ATT - 1.645 * S.E., 
         ci_90_high = ATT + 1.645 * S.E., 
         n = n-10.5)

ggplot(results, aes(n, ATT)) +
  geom_hline(yintercept = 0, lty = 2, color = "gray60") +
  geom_vline(xintercept = 0, lty  =2 ) +
  
  geom_linerange(data = results,
                 aes(x = n, y = ATT, ymin = ci_90_low, ymax = ci_90_high), 
                 size = 0.75, 
                 color = 'gray80', 
                 position = position_dodge(width = 1/2)) +
  
  geom_point(position = position_dodge(width = 1/2), size = 2) + 
  labs(y = "ATT", 
       x = "Time") +
  theme_minimal() + 
  theme(axis.title.y=element_blank(), legend.position = "none") 


#ggsave('./graphs/china/results_cluster_co_0025.pdf', width = 7, height = 3)



plot(m1, type = 'ct') +
  theme_minimal() +
  theme(legend.position = 'bottom') +
  labs(title = "",
       x = 'Treatment Time')

#ggsave('./graphs/china/ct_cluster_co_0025.pdf', width = 7, height = 3)



```


```{r , fig.show='hold', out.width="80%", fig.cap = "Cluster 7", fig.subcap=c('Pre-Treatment period', 'ATT with 90 percent CI', 'Counterfactual')}
# Save plots
knitr::include_graphics(c('./graphs/china/pre_treatment_co_0025.pdf',
                          './graphs/china/results_cluster_co_0025.pdf',
                          './graphs/china/ct_cluster_co_0025.pdf'))
```


# Analysis of China - Varying the cut-off - level 0.1


```{r}
data_china <- read_csv2("./replication_files/data/data/final_data/china_data_with_cluster_co_01.csv") %>% filter(year > 2003)

data_china %>% distinct(ctry1, cluster) %>% arrange(cluster)


```

```{r}
df <- data_china %>% drop_na(IdealPointDistance)

df <- df %>% drop_na(polity2_dist, gdp_cap_dist)

df <- df %>% group_by(ctry1) %>% mutate(n = n()) 


df_sub <- df %>% filter(cluster == 4 & ctry1 != 'Canada')
table <- df_sub %>% select(ctry1) %>% distinct()
```


```{r}
knitr::kable(table)
```


```{r}
# Pre-treatment trends
ggplot(df_sub %>% filter(year %in% c(2004:2013)), aes(year, IdealPointDistance, color = ctry1)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = 'none') +
  
  labs(x = 'Year')

#ggsave('./graphs/china/pre_treatment_co_01.pdf', width = 7, height = 3)


```



```{r, }
#### Reproduces Figure A.10 ####
m1 <- gsynth(IdealPointDistance ~ treated + ctry1_fariss + log(ctry1_gdp_cap) + ctry1_polity2, 
             data = df_sub, 
             index = c("ctry1", "year"),
             se = T,
             force = 'time',
            # CV = TRUE,
             r = c(0, 5),
             inference = "parametric", nboots = 1000,
             parallel = FALSE)


results <- as.tibble(m1$est.att) %>%
  mutate(n = row_number()) %>%
  mutate(ci_90_low = ATT - 1.645 * S.E., 
         ci_90_high = ATT + 1.645 * S.E., 
         n = n-10.5)

ggplot(results, aes(n, ATT)) +
  geom_hline(yintercept = 0, lty = 2, color = "gray60") +
  geom_vline(xintercept = 0, lty  =2 ) +
  
  geom_linerange(data = results,
                 aes(x = n, y = ATT, ymin = ci_90_low, ymax = ci_90_high), 
                 size = 0.75, 
                 color = 'gray80', 
                 position = position_dodge(width = 1/2)) +
  
  geom_point(position = position_dodge(width = 1/2), size = 2) + 
  labs(y = "ATT", 
       x = "Time") +
  theme_minimal() + 
  theme(axis.title.y=element_blank(), legend.position = "none") 


#ggsave('./graphs/china/results_cluster_co_01.pdf', width = 7, height = 3)

plot(m1, type = 'ct') +
  theme_minimal() +
  theme(legend.position = 'bottom') +
  labs(title = "",
       x = 'Treatment Time')

#ggsave('./graphs/china/ct_cluster_co_01.pdf', width = 7, height = 3)

```



```{r , fig.show='hold', out.width="80%", fig.cap = "Cluster 7", fig.subcap=c('Pre-Treatment period', 'ATT with 90 percent CI', 'Counterfactual')}
# Save plots
knitr::include_graphics(c('./graphs/china/pre_treatment_co_01.pdf',
                          './graphs/china/results_cluster_co_01.pdf',
                          './graphs/china/ct_cluster_co_01.pdf'))
```


# Analysis of China - Varying the distance threshold - level 0.1


```{r}
data_china <- read_csv2("./replication_files/data/data/final_data/china_data_with_cluster_th_01.csv") %>% filter(year > 2003)

data_china %>% distinct(ctry1, cluster) %>% arrange(cluster)


```

```{r}
df <- data_china %>% drop_na(IdealPointDistance)

df <- df %>% drop_na(polity2_dist, gdp_cap_dist)

df <- df %>% group_by(ctry1) %>% mutate(n = n()) 


df_sub <- df %>% filter(cluster == 1 & ctry1 != 'Canada')
table <- df_sub %>% select(ctry1) %>% distinct()
```


```{r}
knitr::kable(table)
```

```{r}
# Pre-treatment trends
ggplot(df_sub %>% filter(year %in% c(2004:2013)), aes(year, IdealPointDistance, color = ctry1)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = 'none') +
  
  labs(x = 'Year')

#ggsave('./graphs/china/pre_treatment_th_01.pdf', width = 7, height = 3)

```



```{r, }
#### Reproduces Figure A.12 ####
m1 <- gsynth(IdealPointDistance ~ treated + ctry1_fariss + log(ctry1_gdp_cap) , 
             data = df_sub, 
             index = c("ctry1", "year"),
             se = T,
             force = 'time',
            # CV = TRUE,
             r = c(0, 5),
             inference = "parametric", nboots = 1000,
             parallel = FALSE)

results <- as.tibble(m1$est.att) %>%
  mutate(n = row_number()) %>%
  mutate(ci_90_low = ATT - 1.645 * S.E., 
         ci_90_high = ATT + 1.645 * S.E., 
         n = n-10.5)

ggplot(results, aes(n, ATT)) +
  geom_hline(yintercept = 0, lty = 2, color = "gray60") +
  geom_vline(xintercept = 0, lty  =2 ) +
  
  geom_linerange(data = results,
                 aes(x = n, y = ATT, ymin = ci_90_low, ymax = ci_90_high), 
                 size = 0.75, 
                 color = 'gray80', 
                 position = position_dodge(width = 1/2)) +
  
  geom_point(position = position_dodge(width = 1/2), size = 2) + 
  labs(y = "ATT", 
       x = "Time") +
  theme_minimal() + 
  theme(axis.title.y=element_blank(), legend.position = "none") 

#ggsave('./graphs/china/results_cluster_th_01.pdf', width = 7, height = 3)



plot(m1, type = 'ct') +
  theme_minimal() +
  theme(legend.position = 'bottom') +
  labs(title = "",
       x = 'Treatment Time')

#ggsave('./graphs/china/ct_cluster_th_01.pdf', width = 7, height = 3)
```

```{r , fig.show='hold', out.width="80%", fig.cap = "Cluster 7", fig.subcap=c('Pre-Treatment period', 'ATT with 90 percent CI', 'Counterfactual')}
knitr::include_graphics(c('./graphs/china/pre_treatment_th_01.pdf',
                          './graphs/china/results_cluster_th_01.pdf',
                          './graphs/china/ct_cluster_th_01.pdf'))
```


# Analysis of China - Varying the distance threshold - level 0.5


```{r}
data_china <- read_csv2("./replication_files/data/data/final_data/china_data_with_cluster_th_05.csv") %>% filter(year > 2003)

data_china %>% distinct(ctry1,cluster) %>% arrange(cluster)


```


```{r}
df <- data_china %>% drop_na(IdealPointDistance)

df <- df %>% drop_na(polity2_dist, gdp_cap_dist)

df <- df %>% group_by(ctry1) %>% mutate(n = n()) 


df_sub <- df %>% filter(cluster == 3 & ctry1 != 'Canada')
table <- df_sub %>% select(ctry1) %>% distinct()
```


```{r}
knitr::kable(table)
```

```{r}
# Pre-treatment trends
ggplot(df_sub %>% filter(year %in% c(2004:2013)), aes(year, IdealPointDistance, color = ctry1)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = 'none') +
  
  labs(x = 'Year')

#ggsave('./graphs/china/pre_treatment_th_05.pdf', width = 7, height = 3)


```


```{r, }
#### Reproduces Figure A.13 ####
m1 <- gsynth(IdealPointDistance ~ treated + ctry1_fariss + log(ctry1_gdp_cap) + ctry1_polity2, 
             data = df_sub, 
             index = c("ctry1", "year"),
             se = T,
             force = 'time',
            # CV = TRUE,
             r = c(0, 5),
             inference = "parametric", nboots = 1000,
             parallel = FALSE)


results <- as.tibble(m1$est.att) %>%
  mutate(n = row_number()) %>%
  mutate(ci_90_low = ATT - 1.645 * S.E., 
         ci_90_high = ATT + 1.645 * S.E., 
         n = n-10.5)

ggplot(results, aes(n, ATT)) +
  geom_hline(yintercept = 0, lty = 2, color = "gray60") +
  geom_vline(xintercept = 0, lty  =2 ) +
  geom_linerange(data = results,
                 aes(x = n, y = ATT, ymin = ci_90_low, ymax = ci_90_high), 
                 size = 0.75, 
                 color = 'gray80', 
                 position = position_dodge(width = 1/2)) +
  geom_point(position = position_dodge(width = 1/2), size = 2) + 
  labs(y = "ATT", 
       x = "Time") +
  theme_minimal() + 
  theme(axis.title.y=element_blank(), legend.position = "none") 

#ggsave('./graphs/china/results_cluster_th_05.pdf', width = 7, height = 3)

plot(m1, type = 'ct') +
  theme_minimal() +
  theme(legend.position = 'bottom') +
  labs(title = "",
       x = 'Treatment Time')

#ggsave('./graphs/china/ct_cluster_th_05.pdf', width = 7, height = 3)

```



```{r , fig.show='hold', out.width="80%", fig.cap = "Cluster 7", fig.subcap=c('Pre-Treatment period', 'ATT with 90 percent CI', 'Counterfactual')}
knitr::include_graphics(c('./graphs/china/pre_treatment_th_05.pdf',
                          './graphs/china/results_cluster_th_05.pdf',
                          './graphs/china/ct_cluster_th_05.pdf'))
```

# Analysis of China - Varying the cluster - minimum number of ties 2


```{r}
data_china <- read_csv2("./replication_files/data/data/final_data/china_data_with_cluster_cl_2.csv") %>% filter(year > 2003)

data_china %>% distinct(ctry1, cluster) %>% arrange(cluster)


```


```{r}
df <- data_china %>% drop_na(IdealPointDistance)

df <- df %>% drop_na(polity2_dist, gdp_cap_dist)

df <- df %>% group_by(ctry1) %>% mutate(n = n()) 


df_sub <- df %>% filter(cluster == 5 & ctry1 != 'Canada')
table <- df_sub %>% select(ctry1) %>% distinct()
```


```{r}
knitr::kable(table)
```

```{r}
# Pre-treatment trends
ggplot(df_sub %>% filter(year %in% c(2004:2013)), aes(year, IdealPointDistance, color = ctry1)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = 'none') +
  
  labs(x = 'Year')

#ggsave('./graphs/china/pre_treatment_cl_2.pdf', width = 7, height = 3)


```


```{r, }
#### Reproduces Figure A.14 ####
m1 <- gsynth(IdealPointDistance ~ treated + ctry1_fariss + log(ctry1_gdp_cap) + ctry1_polity2, 
             data = df_sub, 
             index = c("ctry1", "year"),
             se = T,
             force = 'time',
            # CV = TRUE,
             r = c(0, 5),
             inference = "parametric", nboots = 1000,
             parallel = FALSE)

results <- as.tibble(m1$est.att) %>%
  mutate(n = row_number()) %>%
  mutate(ci_90_low = ATT - 1.645 * S.E., 
         ci_90_high = ATT + 1.645 * S.E., 
         n = n-10.5)

ggplot(results, aes(n, ATT)) +
  geom_hline(yintercept = 0, lty = 2, color = "gray60") +
  geom_vline(xintercept = 0, lty  =2 ) +
  geom_linerange(data = results,
                 aes(x = n, y = ATT, ymin = ci_90_low, ymax = ci_90_high), 
                 size = 0.75, 
                 color = 'gray80', 
                 position = position_dodge(width = 1/2)) +
  geom_point(position = position_dodge(width = 1/2), size = 2) + 
  labs(y = "ATT", 
       x = "Time") +
  theme_minimal() + 
  theme(axis.title.y=element_blank(), legend.position = "none") 

#ggsave('./graphs/china/results_cluster_cl_2.pdf', width = 7, height = 3)

plot(m1, type = 'ct') +
  theme_minimal() +
  theme(legend.position = 'bottom') +
  labs(title = "",
       x = 'Treatment Time')

#ggsave('./graphs/china/ct_cluster_cl_2.pdf', width = 7, height = 3)


```


```{r , fig.show='hold', out.width="80%", fig.cap = "Cluster 7", fig.subcap=c('Pre-Treatment period', 'ATT with 90 percent CI', 'Counterfactual')}
knitr::include_graphics(c('./graphs/china/pre_treatment_cl_2.pdf',
                          './graphs/china/results_cluster_cl_2.pdf',
                          './graphs/china/ct_cluster_cl_2.pdf'))
```


# Analysis of China - Varying the cluster - minimum number of ties 5


```{r}
data_china <- read_csv2("./replication_files/data/data/final_data/china_data_with_cluster_cl_5.csv") %>% filter(year > 2003)

data_china %>% distinct(ctry1, cluster) %>% arrange(cluster)


```

```{r}
df <- data_china %>% drop_na(IdealPointDistance)

df <- df %>% drop_na(polity2_dist, gdp_cap_dist)

df <- df %>% group_by(ctry1) %>% mutate(n = n()) 


df_sub <- df %>% filter(cluster == 5 & ctry1 != 'Canada')
table <- df_sub %>% select(ctry1) %>% distinct()
```


```{r}
knitr::kable(table)
```

```{r}
# Pre-treatment trends
ggplot(df_sub %>% filter(year %in% c(2004:2013)), aes(year, IdealPointDistance, color = ctry1)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = 'none') +
  
  labs(x = 'Year')

#ggsave('./graphs/china/pre_treatment_cl_5.pdf', width = 7, height = 3)

```


```{r, }
#### Reproduces Figure A.15 ####
m1 <- gsynth(IdealPointDistance ~ treated + ctry1_fariss + log(ctry1_gdp_cap) + ctry1_polity2, 
             data = df_sub, 
             index = c("ctry1", "year"),
             se = T,
             force = 'time',
            # CV = TRUE,
             r = c(0, 5),
             inference = "parametric", nboots = 1000,
             parallel = FALSE)

results <- as.tibble(m1$est.att) %>%
  mutate(n = row_number()) %>%
  mutate(ci_90_low = ATT - 1.645 * S.E., 
         ci_90_high = ATT + 1.645 * S.E., 
         n = n-10.5)

ggplot(results, aes(n, ATT)) +
  geom_hline(yintercept = 0, lty = 2, color = "gray60") +
  geom_vline(xintercept = 0, lty  =2 ) +
  geom_linerange(data = results,
                 aes(x = n, y = ATT, ymin = ci_90_low, ymax = ci_90_high), 
                 size = 0.75, 
                 color = 'gray80', 
                 position = position_dodge(width = 1/2)) +
  geom_point(position = position_dodge(width = 1/2), size = 2) + 
  labs(y = "ATT", 
       x = "Time") +
  theme_minimal() + 
  theme(axis.title.y=element_blank(), legend.position = "none") 

#ggsave('./graphs/china/results_cluster_cl_5.pdf', width = 7, height = 3)

plot(m1, type = 'ct') +
  theme_minimal() +
  theme(legend.position = 'bottom') +
  labs(title = "",
       x = 'Treatment Time')

#ggsave('./graphs/china/ct_cluster_cl_5.pdf', width = 7, height = 3)

```



```{r , fig.show='hold', out.width="80%", fig.cap = "Cluster 7", fig.subcap=c('Pre-Treatment period', 'ATT with 90 percent CI', 'Counterfactual')}
knitr::include_graphics(c('./graphs/china/pre_treatment_cl_5.pdf',
                          './graphs/china/results_cluster_cl_5.pdf',
                          './graphs/china/ct_cluster_cl_5.pdf'))
```


# conditioning on investments from China


```{r}
data_china <- read_csv2("./replication_files/data/data/final_data/china_data_with_cluster.csv") %>% filter(year > 2003)


data_china <- data_china %>% mutate(sum_invest = ifelse(is.na(sum_invest), 0, sum_invest))

data_china %>% distinct(cluster) %>% arrange(cluster)
```


```{r}
df <- data_china %>% drop_na(IdealPointDistance)

df <- df %>% drop_na(polity2_dist, gdp_cap_dist)

df <- df %>% group_by(ctry1) %>% mutate(n = n()) 


df_sub <- df %>% filter(cluster == 5 & ctry1 != 'Canada')
table <- df_sub %>% select(ctry1) %>% distinct()
```


```{r}
knitr::kable(table)
```

```{r}
# Pre-treatment trends
ggplot(df_sub %>% filter(year %in% c(2004:2013)), aes(year, IdealPointDistance, color = ctry1)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = 'none') +
  labs(x = 'Year')

#ggsave('./graphs/china/pre_treatment_invest.pdf', width = 7, height = 3)

```



```{r, }
#### Reproduces Figure A.16 ####
m1 <- gsynth(IdealPointDistance ~ treated + ctry1_fariss + log(ctry1_gdp_cap) + ctry1_polity2 + sum_invest, 
             data = df_sub, 
             index = c("ctry1", "year"),
             se = T,
             force = 'time',
            # CV = TRUE,
             r = c(0, 5),
             inference = "parametric", nboots = 1000,
             parallel = FALSE)

results <- as.tibble(m1$est.att) %>%
  mutate(n = row_number()) %>%
  mutate(ci_90_low = ATT - 1.645 * S.E., 
         ci_90_high = ATT + 1.645 * S.E., 
         n = n-10.5)

ggplot(results, aes(n, ATT)) +
  geom_hline(yintercept = 0, lty = 2, color = "gray60") +
  geom_vline(xintercept = 0, lty  =2 ) +
  geom_linerange(data = results,
                 aes(x = n, y = ATT, ymin = ci_90_low, ymax = ci_90_high), 
                 size = 0.75, 
                 color = 'gray80', 
                 position = position_dodge(width = 1/2)) +
  geom_point(position = position_dodge(width = 1/2), size = 2) + 
  labs(y = "ATT", 
       x = "Time") +
  theme_minimal() + 
  theme(axis.title.y=element_blank(), legend.position = "none") 

#ggsave('./graphs/china/results_cluster_invest.pdf', width = 7, height = 3)

plot(m1, type = 'ct') +
  theme_minimal() +
  theme(legend.position = 'bottom') +
  labs(title = "",
       x = 'Treatment Time')

#ggsave('./graphs/china/ct_cluster_invest.pdf', width = 7, height = 3)

```



```{r , fig.show='hold', out.width="80%", fig.cap = "Cluster 7", fig.subcap=c('Pre-Treatment period', 'ATT with 90 percent CI', 'Counterfactual')}
knitr::include_graphics(c('./graphs/china/pre_treatment_invest.pdf',
                          './graphs/china/results_cluster_invest.pdf',
                          './graphs/china/ct_cluster_invest.pdf'))
```

```{r, }
# end
```





